package com.smartsofa.main;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smartsofa.R;
import com.smartsofa.photo.GifView;
import com.smartsofa.util.MyUtils;

public class DetailNews extends Activity{	
	
	//定义网络链接地址
	private String url;
	//定义网络连接超时判定变量
	private boolean network_timeout=false;
	private JSONObject json;
	private Boolean flag;

	//加载动画
	private LinearLayout lay_loading_gif;
	private GifView loading_gif;
	private Button bt_back;
	private TextView tv_news_title;
	private TextView tv_news_time;
	private TextView tv_news_content;
	private View title_bar_divider;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.detail_news); 
		initView();		
	}

	private void initView(){
		
		//接收参数
		Bundle bundle=getIntent().getExtras();
		long newsid=bundle.getLong("newsid");
		//加载动画
		lay_loading_gif=(LinearLayout) findViewById(R.id.lay_loading_gif);
		loading_gif=(GifView) findViewById(R.id.detail_loading);
		loading_gif.setMovieResource(R.raw.loading);
		lay_loading_gif.setVisibility(View.VISIBLE);
		//分割线
		title_bar_divider=findViewById(R.id.title_bar_divider);
		tv_news_title=(TextView) findViewById(R.id.tv_news_title);
		tv_news_time=(TextView) findViewById(R.id.tv_news_time);
		tv_news_content=(TextView) findViewById(R.id.tv_news_content);
		bt_back=(Button) findViewById(R.id.detailnews_back);
		bt_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DetailNews.this.finish();
			}
		});
		//开启线程加载数据
		Thread content_thread=new Thread(new loadNewsThread(newsid));
		content_thread.start();
	}
	
	class loadNewsThread implements Runnable{
		long newsId;
		public loadNewsThread(long newsid2){
			this.newsId=newsid2;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try{
				url=MyUtils.getUrl()+"knowledge_getDetailNews?newsId="+newsId;
				Log.i("url:", url);
				HttpGet request = new HttpGet(url);																
				BasicHttpParams httpParams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(httpParams,MyUtils.REQUEST_TIMEOUT);
			    HttpClient httpClient = new DefaultHttpClient(httpParams);
	            HttpResponse httpResponse = httpClient.execute(request);							       
		       	
		      if (httpResponse.getStatusLine().getStatusCode() == 200) {
		    	network_timeout=true;
		        String jsonString = EntityUtils.toString(httpResponse.getEntity());
		        json = new JSONObject(jsonString);
		        flag=(Boolean) json.get("success");			        				      		       
		      }
			}catch(Exception e){
				e.printStackTrace();
			}
			Message msg = content_handler.obtainMessage();
			if(network_timeout){
				if (flag) {				
					msg.what = 1;									
				}else{
					msg.what = 2;			
				}
			}			
			else{
				msg.what = 3;						
			}
			content_handler.sendMessage(msg);
		}
		
	}
	
	Handler content_handler = new Handler() {
		public void handleMessage(Message msg) {	
			lay_loading_gif.setVisibility(View.GONE);			
			switch (msg.what) {		
			case 1:
				title_bar_divider.setVisibility(View.VISIBLE);
				try {
					JSONObject jsonobj=new JSONObject();
					jsonobj=json.getJSONObject("obj");
					tv_news_title.setText(jsonobj.getString("infoTitle"));
					tv_news_time.setText(jsonobj.getString("infoTime"));
					tv_news_content.setText(jsonobj.getString("contentBody").replaceAll("\\s*", ""));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
				break;
			case 2:
				Toast.makeText(DetailNews.this, "数据加载失败，请稍后重试！", Toast.LENGTH_SHORT).show();
				break;
			case 3:
				Toast.makeText(DetailNews.this, "网络连接超时！", Toast.LENGTH_SHORT).show();
				break;
			}
		}		
	};
	
}
